package com.taotao.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.utils.FastDFSClient;
import com.taotao.utils.JsonUtils;



/**
 * 图片上传Controller
 * @author Administrator
 *
 */
@Controller
public class PictureController {

	@Value("${IMAGE_SERVER_URL}")
	private String IMAGE_SERVER_URL;
	
	@RequestMapping("/pic/upload")
	@ResponseBody
	public String picUpload(MultipartFile uploadFile){
		Map result=new HashMap<String,Object>();
		try {
			//接受上传的文件
			//取拓展名
			String originalFileName=uploadFile.getOriginalFilename();
			String extName=originalFileName.substring(originalFileName.lastIndexOf(".")+1);
			//上传到图片服务器
			FastDFSClient fastDFSClient=new FastDFSClient("C:/Users/Administrator/git/taotao/taotao-manager-web/src/main/resources/resource/client.conf");
			//相应上传图片的url
			String url=fastDFSClient.uploadFile(uploadFile.getBytes(),extName,null);
			url=IMAGE_SERVER_URL+url;
			result.put("error", 0);
			result.put("url",url);
		} catch (Exception e) {
			result.put("error",1);
			result.put("message","图片上传失败");
			e.printStackTrace();
		}
		
		return JsonUtils.objectToJson(result) ;
	}
}
